package com.dark.cms.base.dto;

import com.dark.cms.base.utils.JSONUtil;
import com.dark.cms.base.utils.ValidateUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 响应消息结构体
 * 状态码 00 业务处理成功|| 01 业务处理失败 || 02 登录已失效
 * @author Devil
 * 
 */

public class ResDTO {
	/**
	 * 返回值含义
	 */
	public enum CODETYPE{

		B_SUCCESS("00"),  //业务处理成功
		B_FAILURE("01"), //业务处理失败
		L_INVALID("02"); //登录已失效

		private String value;

		CODETYPE(String value){
			this.value = value;
		}

		public String value() {
			return value;
		}
	}

	// 前台响应消息
	private String msg;
	// 响应代码
	private CODETYPE code;
	// 设置form表单
	private BaseDTO formData;
	// 表格数据
	private Map gridData;

	public ResDTO() {
		msg = "";
		code = CODETYPE.B_SUCCESS; // 正常状态
		formData = new BaseDTO();
		gridData = new HashMap();
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCode() {
		return code.value;
	}

	public void setCode(CODETYPE code) {
		this.code = code;
	}

	public Map getGridData() {
		return gridData;
	}
	
	public void setGridData(String id, List list) {
		if (ValidateUtil.isNotEmpty(id) && list != null) {
			this.gridData.put(id, list);
		}
	}
	
	public void setGridData(String id, PageDTO dto) {
		if (ValidateUtil.isNotEmpty(id) && dto != null) {
			this.gridData.put(id, dto);
		}
	}

	public BaseDTO getFormData() {
		return formData;
	}

	public void setFormData(Map formData) {
		if (!ValidateUtil.isEmpty(formData)) {
			this.formData.putAll(formData);
		}
	}

	public void setFormData(String id, Object value) {
		if (ValidateUtil.isNotEmpty(id) && !ValidateUtil.isEmpty(value)) {
			this.formData.put(id, value);
		}
	}

	@Override
	public String toString() {
		Map retMap = new HashMap();
		retMap.put("msg", getMsg());
		retMap.put("code", getCode());
		retMap.put("formData", getFormData());
		retMap.put("gridData", getGridData());
		return JSONUtil.toJSONString(retMap);
	}
}
